package com.ll.gulimall.coupon.service.impl;

import com.ll.common.to.MemberPrice;
import com.ll.common.to.SkuMemberTo;
import com.ll.gulimall.coupon.entity.MemberPriceEntity;
import com.ll.gulimall.coupon.entity.SkuFullReductionEntity;
import com.ll.gulimall.coupon.service.MemberPriceService;
import com.ll.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ll.common.utils.PageUtils;
import com.ll.common.utils.Query;

import com.ll.gulimall.coupon.dao.SkuLadderDao;
import com.ll.gulimall.coupon.entity.SkuLadderEntity;
import com.ll.gulimall.coupon.service.SkuLadderService;


@Service("skuLadderService")
public class SkuLadderServiceImpl extends ServiceImpl<SkuLadderDao, SkuLadderEntity> implements SkuLadderService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuLadderEntity> page = this.page(
                new Query<SkuLadderEntity>().getPage(params),
                new QueryWrapper<SkuLadderEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public void saveSkuMember(SkuMemberTo skuMemberTo) {
        //6.4)、sku的优惠、满减等信息;guLimalLl_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setAddOther(skuMemberTo.getCountStatus());
        skuLadderEntity.setDiscount(skuMemberTo.getDiscount());
        skuLadderEntity.setFullCount(skuMemberTo.getFullCount());
        skuLadderEntity.setSkuId(skuMemberTo.getSkuId());
        if (skuLadderEntity.getFullCount() > 0) {
            //保存有意义的数据
            skuLadderService.save(skuLadderEntity);
        }


        //sms_sku_full_reduction
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuMemberTo, reductionEntity);
        if (reductionEntity.getFullPrice().compareTo(BigDecimal.valueOf(0)) == 1) {
            //保存有意义的数据
            skuFullReductionService.save(reductionEntity);
        }


        //sms_member_price

        List<MemberPrice> memberPrice = skuMemberTo.getMemberPrice();

        List<MemberPriceEntity> collect = memberPrice.stream().map((item) -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuMemberTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item -> {
            //保存有意义的数据
            return item.getMemberPrice().compareTo(BigDecimal.valueOf(0)) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);


    }

}